package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Resource
    private DeptService deptService;
    @Resource
    private EmpExprService empExprService;

    @Override
    public PageResult<Emp> getPageResult(EmpQueryParam param) {


        Page<Emp> page =Page.of(param.getPage(),param.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(param.getName() !=null&&!param.getName().isEmpty(),Emp::getName,param.getName())
                .eq(param.getGender()!=null,Emp::getGender,param.getGender())
                .between(param.getBegin()!=null&&param.getEnd()!=null,Emp::getEntryDate,param.getBegin(),param.getEnd());

        page=page(page,queryWrapper);
        List<Emp> emps = page.getRecords();
        List<Integer> deptIds = emps.stream().map(emp -> emp.getDeptId()).collect(Collectors.toList());
        List<Dept> depts = deptService.listByIds(deptIds);
        Map<Integer,String> deptNames = depts.stream().collect(Collectors.toMap(Dept::getId,Dept::getName));
        emps.forEach(emp -> emp.setDeptName(deptNames.get(emp.getDeptId())));

        return new PageResult<>(page.getTotal(),page.getRecords());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEmp(Emp emp) {

        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        save(emp);

        List<EmpExpr> empExprs =emp.getExprList();
        empExprs.forEach(expr ->expr.setEmpId(emp.getId()));
        empExprService.saveBatch(empExprs);

    }

    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = getById(id);
        List<EmpExpr> list = empExprService.list(new LambdaQueryWrapper<EmpExpr>()
                .eq(EmpExpr::getEmpId, id));
        emp.setExprList(list);
        return emp;
    }

    @Override
    public void updateEmp(Emp emp) {

        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        empExprService.remove( new LambdaQueryWrapper<EmpExpr>()
                .eq(EmpExpr::getEmpId,emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();

        if(exprList !=null&& !exprList.isEmpty()){
            exprList.forEach(expr->expr.setEmpId(emp.getId()));
        }
        empExprService.saveBatch(exprList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeEmpsByIds(List<Integer> ids) {
        if(ids==null || ids.isEmpty()){
            return;
        }
        removeBatchByIds(ids);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>()
                .in(EmpExpr::getEmpId,ids));
    }
}
